package com.qinbaokeji.checkpoint.dahuacamera.config;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qinbaokeji.checkpoint.dahuacamera.entity.CameraInfo;
import com.qinbaokeji.checkpoint.dahuacamera.service.ICameraInfoService;
import org.apache.commons.io.output.TeeOutputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import java.io.FileOutputStream;
import java.io.PrintStream;

//@Configuration
public class StartConfig {

    @Autowired
    private ApplicationArguments applicationArguments;
    @Autowired
    private ICameraInfoService iCameraInfoService;
    @Autowired
    private DahuaServerConfig dahuaServerConfig;


//    @Bean
    public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer(){
        String uuid = "";
        if(StringUtils.isEmpty(dahuaServerConfig.getStartUuid())){
            uuid = applicationArguments.getNonOptionArgs().get(0);
        }else {
            uuid = dahuaServerConfig.getStartUuid();
        }
        System.out.println(uuid);
        LambdaQueryWrapper<CameraInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CameraInfo::getUuid,uuid);
        CameraInfo cameraInfo = iCameraInfoService.getOne(wrapper);

//        return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
//            @Override
//            public void customize(ConfigurableWebServerFactory factory) {
//                factory.setPort(8081);
//            }
//        };
        //直接lambda
        return factory -> factory.setPort(cameraInfo.getServerPort());

    }


}
